package com.hrbxlh.action;
import java.awt.Color;
import java.io.*;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;

import com.hrbxlh.util.PubMethods;
import org.apache.log4j.Logger;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.html.simpleparser.StyleSheet;
import com.lowagie.text.rtf.RtfWriter2;

public class ITextWord {
    private Font font2;
    //日志信息
    Logger logger = Logger.getLogger(ITextWord.class.getName());
    public static ITextWord getInstance() throws Exception {
        return new ITextWord();
    }
    public void createRtfContext(Map<String,Object> resultMap, OutputStream out, String type, Map<String,Object> paramMap) {
        Document doc = new Document(PageSize.A4);
        //文件名
        String fileName = String.valueOf(resultMap.get("fileName"));
        //单选选择集合
        List<Map<String, Object>> optionList = (List<Map<String, Object>>) resultMap.get("optionList");
        //多选选择集合
        List<Map<String, Object>> manyOptionList = (List<Map<String, Object>>) resultMap.get("manyOptionList");
        //判断集合
        List<Map<String, Object>> judgeList = (List<Map<String, Object>>) resultMap.get("judgeList");
        //填空集合
        List<Map<String, Object>> blanksList = (List<Map<String, Object>>) resultMap.get("blanksList");
        //简答集合
        List<Map<String, Object>> answerList = (List<Map<String, Object>>) resultMap.get("answerList");
        //课程内容
        Map<String, Object> courseMap = (Map<String, Object>) resultMap.get("courseMap");
        //内容计数
        int contentCount=0;
        //标题计数
        int titleCount=0;
        logger.info("单选选择"+optionList);
        logger.info("多选选择"+manyOptionList);
        logger.info("判断"+judgeList);
        logger.info("填空"+blanksList);
        logger.info("简答"+answerList);
        //分数
        float optionSorce = 0;
        float manyOptionSorce = 0;
        float judgeSource = 0;
        float blanksSource = 0;
        float answerSource = 0;
        try {
            //创建RtfWriter对象
            RtfWriter2.getInstance(doc, out);
            doc.open();
            Paragraph title1 = null;
            if(courseMap!=null){
                //选择大标题
                String titleStr = fileName;
                if(fileName.contains(".")){
                    fileName  = fileName.substring(0,fileName.lastIndexOf("."));
                }
                logger.info("生成什么课程的试题啊"+titleStr);
                Paragraph p = new Paragraph(titleStr, new Font(Font.NORMAL, 16, Font.BOLD, new Color(0, 0, 0)));
                p.setAlignment(1);
                doc.add(p);
                //标题字体风格
                Font tf = new Font();
                tf.setStyle(Font.BOLD);
                // 换行
                title1 = new Paragraph("\n");
                doc.add(title1);
            }

            if(optionList!=null&&optionList.size()!=0){
                //选择取标题
                Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap");
                if(!String.valueOf(titleMap.get("optionTitle")).equals("")&&titleMap.get("optionTitle")!=null){
                    titleCount+=1;
                    //算分数
                    optionSorce = Float.parseFloat(String.valueOf(paramMap.get("optionSorce")))*(optionList.size());
//                    String titleSorce ="(每题"+String.valueOf(paramMap.get("optionSorce"))+"分,共"+optionSorce+"分)";
                    String titleSorce ="(每题"+String.valueOf(paramMap.get("optionSorce"))+"分,共"+optionList.size()+"题)";
                    String titleName = "一."+(String)titleMap.get("optionTitle")+titleSorce;
                    // 标题
                    Paragraph title = new Paragraph(titleName, new Font(Font.NORMAL, 12, Font.BOLD, new Color(0, 0, 0)));
                    title.setAlignment(Element.ALIGN_LEFT);
                    doc.add(title);
                    // 换行
                   /* title1 = new Paragraph("\n");
                    doc.add(title1);*/
                }


                //循环选择集合
                for (int i = 0; i < optionList.size(); i++) {
                    Map<String,Object> questionMap = optionList.get(i);
                    // 正文
                    int countI = i+1;
                    String name = String.valueOf(countI)+"."+(String)questionMap.get("questionName");
                    name = PubMethods.isContailKh(name);
                    Paragraph questionName = new Paragraph(name, new Font(Font.NORMAL, 12, Font.NORMAL, new Color(0, 0, 0)));
                    questionName.setAlignment(Element.ALIGN_LEFT);
                    doc.add(questionName);
                    String optionMsg = (String)questionMap.get("optionMsg");
//		                optionMsg = optionMsg.replaceAll("<br>", "\n");
                    optionMsg = optionMsg.replaceAll("<br>", "\0");
                    logger.info("选项"+optionMsg);
                    String[] optionMsgArr = new String[4];
                    if(optionMsg.contains("#")){
                        optionMsgArr = optionMsg.split("#");
                    } /*if(optionMsg.contains(",")){
                        optionMsgArr = optionMsg.split(",");
                    }*/
                    for(int j=0;j<optionMsgArr.length;j++){
                        String optionMsgArrStr="";
                        if(optionMsgArr[j]!=null){
                            optionMsgArrStr = optionMsgArr[j].trim();
                        }else{
                            optionMsgArrStr = optionMsgArr[j];
                        }
                        Paragraph finalOptionMsg = new Paragraph(optionMsgArrStr, new Font(Font.NORMAL, 12, Font.NORMAL, new Color(0, 0, 0)));
                        finalOptionMsg.setAlignment(Element.ALIGN_LEFT);
                        doc.add(finalOptionMsg);
                        if(j==optionMsgArr.length-1){
                            // 换行
                            title1 = new Paragraph("\n");
                            doc.add(title1);
                        }
                    }
                    contentCount = countI;
                }
            }
//****************************************************************
            //多选
            if(manyOptionList!=null&&manyOptionList.size()!=0){
                String titleName = "";
                //选择取标题
                Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap");
                if(!String.valueOf(titleMap.get("manyOptionTitle")).equals("")&&titleMap.get("manyOptionTitle")!=null){
                    //算分数
                    manyOptionSorce = Float.parseFloat(String.valueOf(paramMap.get("manyOptionSorce")))*(manyOptionList.size());
//                    String titleSorce ="(每题"+String.valueOf(paramMap.get("manyOptionSorce"))+"分,共"+manyOptionSorce+"分)";
                    String titleSorce ="(每题"+String.valueOf(paramMap.get("manyOptionSorce"))+"分,共"+manyOptionList.size()+"题)";
                    if(titleCount>0){
                        //说明有单选择题
                        titleName = "二."+String.valueOf(titleMap.get("manyOptionTitle"))+titleSorce;
                    }else{
                        //说明没有多选择题
                        titleName =  "一."+String.valueOf(titleMap.get("manyOptionTitle"))+titleSorce;
                    }
                    // 标题
                    Paragraph title = new Paragraph(titleName, new Font(Font.NORMAL, 12, Font.BOLD, new Color(0, 0, 0)));
                    title.setAlignment(Element.ALIGN_LEFT);
                    doc.add(title);
                    titleCount+=1;
                    // 换行
                   /* title1 = new Paragraph("\n");
                    doc.add(title1);*/
                }


                //循环选择集合
                for (int i = 0; i < manyOptionList.size(); i++) {
                    Map<String,Object> questionMap = manyOptionList.get(i);
                    // 正文
                    int countI = i+1;
                    String name = String.valueOf(countI)+"."+String.valueOf(questionMap.get("questionName"));
                    name = PubMethods.isContailKh(name);
                    Paragraph questionName = new Paragraph(name, new Font(Font.NORMAL, 12, Font.NORMAL, new Color(0, 0, 0)));
                    questionName.setAlignment(Element.ALIGN_LEFT);
                    doc.add(questionName);
                    String optionMsg = String.valueOf(questionMap.get("optionMsg"));
                    optionMsg = optionMsg.replaceAll("<br>", "\0");
                    logger.info("选项"+optionMsg);
                    String[] optionMsgArr = {};
                    if(optionMsg.contains("#")){
                        optionMsgArr = optionMsg.split("#");
                    }
                    for(int j=0;j<optionMsgArr.length;j++){
                        String optionMsgArrStr="";
                        if(optionMsgArr[j]!=null){
                            optionMsgArrStr = optionMsgArr[j].trim();
                        }else{
                            optionMsgArrStr = optionMsgArr[j];
                        }
                        Paragraph finalOptionMsg = new Paragraph(optionMsgArrStr, new Font(Font.NORMAL, 12, Font.NORMAL, new Color(0, 0, 0)));
                        finalOptionMsg.setAlignment(Element.ALIGN_LEFT);
                        doc.add(finalOptionMsg);
                        if(j==optionMsgArr.length-1){
                            // 换行
                            title1 = new Paragraph("\n");
                            doc.add(title1);
                        }
                    }
                    contentCount = countI;
                }
            }














            if(judgeList!=null&&judgeList.size()!=0){
                logger.info("判断题号"+titleCount);
                //判断取标题
                Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap");
                if(!String.valueOf(titleMap.get("judgeTitle")).equals("")&&titleMap.get("judgeTitle")!=null){
                    String titleName = "";
                    //算分数
                    judgeSource = Float.parseFloat(String.valueOf(paramMap.get("judgeSource")))*(judgeList.size());
//                    String titleSorce ="(每题"+String.valueOf(paramMap.get("judgeSource"))+"分,共"+judgeSource+"分)";
                    String titleSorce ="(每题"+String.valueOf(paramMap.get("judgeSource"))+"分,共"+judgeList.size()+"题)";
                    if(titleCount>0&&titleCount>1){
                        //说明既有单选择题又有多选择题、
                        titleName = "三."+String.valueOf(titleMap.get("judgeTitle"))+titleSorce;
                    }if(titleCount>0&&titleCount<=1){
                        //说明前面只有一道题、
                        titleName = "二."+(String.valueOf(titleMap.get("judgeTitle")))+titleSorce;
                    }if(titleCount==0){
                        //说明前面一道题也没有、
                        titleName = "一."+String.valueOf(titleMap.get("judgeTitle"))+titleSorce;
                    }
                    // 标题
                    Paragraph title = new Paragraph(titleName, new Font(Font.NORMAL, 12, Font.BOLD, new Color(0, 0, 0)));
                    title.setAlignment(Element.ALIGN_LEFT);
                    doc.add(title);
                    // 换行
                   /* title1 = new Paragraph("\n");
                    doc.add(title1);*/
                    titleCount+=1;
                }

                //循环判断集合
                for (int i = 0; i < judgeList.size(); i++) {
                    Map<String,Object> questionMap = judgeList.get(i);
                    int countI = i+1;
//                    contentCount+=1;
                    String name = String.valueOf(countI)+"."+(String)questionMap.get("questionName");
                    name = PubMethods.isContailKh(name);
                    // 正文
                    Paragraph questionName = new Paragraph(name, new Font(Font.NORMAL, 12, Font.NORMAL, new Color(0, 0, 0)));
                    questionName.setAlignment(Element.ALIGN_LEFT);
                    doc.add(questionName);
                    if(i==judgeList.size()-1){
                        // 换行
                        title1 = new Paragraph("\n");
                        doc.add(title1);
                    }
                }
            }

            if(blanksList!=null&&blanksList.size()!=0){
                logger.info("填空题号"+titleCount);
                //填空取标题
                Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap");
                if((String)titleMap.get("blanksTitle")!=""&&titleMap.get("blanksTitle")!=null){
                    String titleName = "";
                    //算分数
                    blanksSource = Float.parseFloat(String.valueOf(paramMap.get("blanksSource")))*(blanksList.size());
//                    String titleSorce ="(每题"+String.valueOf(paramMap.get("blanksSource"))+"分,共"+blanksSource+"分)";
                    String titleSorce ="(每题"+String.valueOf(paramMap.get("blanksSource"))+"分,共"+blanksList.size()+"题)";
                    if(titleCount>=3){
                        //说明有单选选择题又、多选选择题、判断题还有填空题、
                        titleName = "四."+String.valueOf(titleMap.get("blanksTitle"))+titleSorce;
                    }if(titleCount>0&&titleCount<=1){
                        //说明前面只有一道题、
                        titleName = "二."+String.valueOf(titleMap.get("blanksTitle"))+titleSorce;
                    }if(titleCount==0){
                        //说明前面一道题也没有、
                        titleName = "一."+String.valueOf(titleMap.get("blanksTitle"))+titleSorce;
                    }if(titleCount>=2&&titleCount<3){
                        //说明前面有两道题、
                        titleName = "三."+String.valueOf(titleMap.get("blanksTitle"))+titleSorce;
                    }
                    // 标题
                    Paragraph title = new Paragraph(titleName, new Font(Font.NORMAL, 12, Font.BOLD, new Color(0, 0, 0)));
                    title.setAlignment(Element.ALIGN_LEFT);
                    doc.add(title);
                    // 换行
                    /*title1 = new Paragraph("\n");
                    doc.add(title1);*/
                    titleCount+=1;
                }


                //循环填空集合
                for (int i = 0; i < blanksList.size(); i++) {
                    Map<String,Object> questionMap = blanksList.get(i);
                    int countI = i+1;
//                    contentCount+=1;
                    String name = String.valueOf(countI)+"."+String.valueOf(questionMap.get("questionName"));
                    name =name.replace("(+)","()");
                    name = PubMethods.isContailKh(name);
                    // 正文
                    Paragraph questionName = new Paragraph(name, new Font(Font.NORMAL, 12, Font.NORMAL, new Color(0, 0, 0)));
                    questionName.setAlignment(Element.ALIGN_LEFT);
                    doc.add(questionName);
                    if(i==blanksList.size()-1){
                        // 换行
                        title1 = new Paragraph("\n");
                        doc.add(title1);
                    }
                }
            }

            if(answerList!=null&&answerList.size()!=0){
                logger.info("简答题号"+titleCount);
                //循环简答集合取标题
                Map<String,Object> titleMap = (Map<String, Object>) resultMap.get("titleMap");
                if(!String.valueOf(titleMap.get("answerTitle")).equals("")&&titleMap.get("answerTitle")!=null){
                    String titleName = "";
                    //算分数
                    answerSource = Float.parseFloat(String.valueOf(paramMap.get("answerSource")))*(answerList.size());
//                    String titleSorce ="(每题"+String.valueOf(paramMap.get("answerSource"))+"分,共"+answerSource+"分)";
                    String titleSorce ="(每题"+String.valueOf(paramMap.get("answerSource"))+"分,共"+answerList.size()+"题)";
                    if(titleCount>=4){
                        //说明全部题型都有、
                        titleName = "五."+String.valueOf(titleMap.get("answerTitle"))+titleSorce;
                    }
                    if(titleCount>=3&&titleCount<4){
                        //说明有单选选择题、多选、判断题还有填空题、
                        titleName = "四."+String.valueOf(titleMap.get("answerTitle"))+titleSorce;
                    }if(titleCount>0&&titleCount<=1){
                        //说明前面只有一道题、
                        titleName = "二."+String.valueOf(titleMap.get("answerTitle"))+titleSorce;
                    }if(titleCount==0){
                        //说明前面一道题也没有、
                        titleName = "一."+String.valueOf(titleMap.get("answerTitle"))+titleSorce;
                    }if(titleCount>=2&&titleCount<3){
                        //说明前面有两道题、
                        titleName = "三."+String.valueOf(titleMap.get("answerTitle"))+titleSorce;
                    }
                    // 标题
                    Paragraph title = new Paragraph(titleName, new Font(Font.NORMAL, 12, Font.BOLD, new Color(0, 0, 0)));
                    title.setAlignment(Element.ALIGN_LEFT);
                    doc.add(title);
                    // 换行
                   /*title1 = new Paragraph("\n");
                    doc.add(title1);*/
                    titleCount+=1;
                }

                //循环简答集合
                for (int i = 0; i < answerList.size(); i++) {
                    Map<String,Object> questionMap = answerList.get(i);
                    int countI = i+1;
//                    contentCount+=1;
                    String name = String.valueOf(countI)+"."+(String)questionMap.get("questionName");
                    // 正文
                    Paragraph questionName = new Paragraph(name, new Font(Font.NORMAL, 12, Font.NORMAL, new Color(0, 0, 0)));
                    questionName.setAlignment(Element.ALIGN_LEFT);
                    doc.add(questionName);

                    // 换行
                    title1 = new Paragraph("\n\n\n\n");
                    doc.add(title1);

                }
            }
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public static void addHtmlContent(Object element, String html, String root, float imgWidth) throws IOException, DocumentException{
        StyleSheet ss = new StyleSheet();
        html = html.replace("..", root);
        List<?> htmlList = HTMLWorker.parseToList(new StringReader(html), ss);
        for (int i = 0 ; i < htmlList.size(); i++){
            Element e = (Element) htmlList.get(i);
            for (Object object : e.getChunks()) {
                if(object instanceof Chunk){
                    Chunk ch = (Chunk) object;
                    if(ch.getImage() != null){
                        float width = imgWidth;
                        float height = ch.getImage().getHeight()/ch.getImage().getWidth()*width;
                        ch.getImage().scaleAbsolute(width,height);
                    }
                }
            }
            if(element instanceof Cell){
                ((Cell)element).add(e);
            }
            if(element instanceof Paragraph){
                ((Paragraph)element).add(e);
            }
            if(element instanceof Document){
                ((Document)element).add(e);
            }
        }
    }
}
